cmake_minimum_required(VERSION 3.19)
project(Engine VERSION 1.0 LANGUAGES CXX)

# Set Conan-generated CMake toolchain
include(${CMAKE_SOURCE_DIR}/Targets/ConanFiles/${CMAKE_BUILD_TYPE}/conan_toolchain.cmake)

# Gather all source files
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE headers ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
#add_library(${PROJECT_NAME} SHARED ${sources} ${headers})

add_executable(${PROJECT_NAME} ${sources} ${headers})
# Export engine symbols
target_compile_definitions(${PROJECT_NAME} PUBLIC 
    -DTRYENGINGE_EXPORT
)
target_compile_definitions(${PROJECT_NAME} PRIVATE FMT_HEADER_ONLY)


# Include directories
target_include_directories(${PROJECT_NAME} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 添加预编译头文件
target_precompile_headers(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/pch.h)

find_package(glm REQUIRED)
find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)


target_link_libraries(${PROJECT_NAME} PUBLIC
    glm::glm
    glfw
    GLEW::GLEW  # Ensure correct target name
)

if (MSVC)

    # 定义一个函数来处理 DLL 文件
    function(copy_dlls_from_include_dir include_dir)
            get_filename_component(BIN_DIR "${include_dir}/.." ABSOLUTE)
            set(DLL_SEARCH_DIR "${BIN_DIR}/bin")    
            # 查找 DLL 文件
            file(GLOB DLLS "${DLL_SEARCH_DIR}/*.dll")
                # 复制 DLL 文件到目标目录
                foreach(dll IN LISTS DLLS)
                    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 
                        COMMAND ${CMAKE_COMMAND} -E copy_if_different 
                        ${dll} $<TARGET_FILE_DIR:${PROJECT_NAME}>)
                endforeach()


    endfunction()
        
    # 调用函数处理 yaml-cpp 的 DLL 文件
    message(WARNING "[WARNING] Searching for yaml-cpp DLLs in: ${yaml-cpp_INCLUDE_DIR}")
    if(EXISTS(${yaml-cpp_INCLUDE_DIR}))
        copy_dlls_from_include_dir(${yaml-cpp_INCLUDE_DIR})
    endif()

    # 调用函数处理 PhysX 的 DLL 文件
    message(WARNING "[WARNING] Searching for PhysX DLLs in: ${PhysX_INCLUDE_DIR}")
    if(EXISTS(${PhysX_INCLUDE_DIR}))
        copy_dlls_from_include_dir(${PhysX_INCLUDE_DIR})
    endif()

endif()


# Copy resources
if(EXISTS ${CMAKE_SOURCE_DIR}/Resources)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND 
        ${CMAKE_COMMAND} -E copy_directory 
        ${CMAKE_SOURCE_DIR}/Resources
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/Resources
    )
else()
    message(WARNING "[WARNING] no resource directory!")
endif()

# Copy script directory
if(EXISTS ${CMAKE_SOURCE_DIR}/TryScript)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND 
        ${CMAKE_COMMAND} -E copy_directory 
        ${CMAKE_SOURCE_DIR}/TryScript
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/Resources/Scripts
    )
else()
    message(WARNING "[WARNING] no script directory!")
endif()
